package cn.hzw.supermarket.activity;

import static cn.hzw.supermarket.util.HttpUtil.sendHttpRequest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

import cn.hzw.supermarket.R;
import cn.hzw.supermarket.databinding.ActivityUpdatePhoneBinding;
import cn.hzw.supermarket.databinding.ActivityUpdateUsernameBinding;
import cn.hzw.supermarket.domain.User;
import cn.hzw.supermarket.model.ResultInfo;
import cn.hzw.supermarket.util.StringUtils;

public class UpdatePhoneActivity extends AppCompatActivity {

    private ActivityUpdatePhoneBinding binding;
    String phone;
    public static User user=new User();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //全屏，隐藏标题
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        binding = ActivityUpdatePhoneBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        binding.etPhone.setText(bundle.getString("phone"));
        binding.layoutTitle.tvTitle.setText("修改手机号");
        binding.layoutTitle.ivBack.setOnClickListener(view -> {
            finish();
        });
        binding.tvSave.setOnClickListener(view -> {
            phone = binding.etPhone.getText().toString();
            if (phone.isEmpty()){
                Toast.makeText(this, "手机号不能为空", Toast.LENGTH_SHORT).show();
            }else if (!StringUtils.isMobilePhone(phone)){
                Toast.makeText(this, "手机号格式错误", Toast.LENGTH_SHORT).show();
            }
            else {
                postRequest(phone,LoginActivity.user.getId());
            }
        });
    }
    public static ResultInfo resultInfo;
    private void postRequest(String phone, Integer id) {
        new Thread(() -> {
            Map<String, String> map = new HashMap<>();
            map.put("phone", phone);
            map.put("id", String.valueOf(id));
            String response = sendHttpRequest("/user/updatePhone", map);
            Log.d("TAG", "postRequest: "+response);
            if(response!=null&&!response.isEmpty()){
                resultInfo = JSONObject.parseObject(response, ResultInfo.class);//将JSON字符串转换成对象
                if(resultInfo.isSuccess()){
                    user = resultInfo.getUser();
                    Log.d("TAG", "postRequest:sd22332 "+user);
                    LoginActivity.user.setPhone(resultInfo.getUser().getPhone());
                    Intent intent=new Intent(this,UserInfoActivity.class);
                    startActivity(intent);
                    UpdatePhoneActivity.this.finish();
                } else
                    runOnUiThread(() -> Toast.makeText(this, "修改失败", Toast.LENGTH_LONG).show());
            }
        }).start();
    }
}